#include <stdio.h>
#include <stdlib.h>

typedef struct listnode
{
    int num;
    struct listnode * next;
}listnode;

//创建新链表
int creatlist(listnode ** list)
{
    //创建头节点
    *list = (listnode*)malloc(sizeof(listnode));
    if (NULL == *list)
    {
        printf("头节点创建失败");
        return EOF;
    }
    //2.初始化头节点
    (*list)->next = NULL;
    return 0;
}
//打印链表
int printlist(listnode * list)
{
    if (NULL == list)
    {
        printf("传入的头节点 为 NULL");
        return EOF;
    }

    if (NULL == list->next)
    {
        printf("链表内无数据");
        return EOF;
    }
    listnode * ptr_node = list->next;
    for (size_t i = 0; NULL != ptr_node; i++)
    {
        printf("%d",ptr_node->num);
        ptr_node = ptr_node->next;
    }
    return 0;
}
int freelist(listnode ** list)
{
    if (NULL == *list)
    {
        printf("传入的头节点 为 NULL");
        return EOF;
    }

    listnode * ptr_node_1 = (*list)->next;
    listnode * ptr_node_2 = ptr_node_1->next;
    for (size_t i = 0; NULL != ptr_node_1; i++)
    {
        free(ptr_node_1);
        ptr_node_1 = ptr_node_2;

        if (NULL != ptr_node_2)
        {
            ptr_node_2 = ptr_node_2->next;
        }
    }
    free(*list);
    *list = NULL;
    return 0;
}
//合并
int mergelist(listnode *list1,listnode *list2)
{
    if (NULL == list1 || NULL == list2)
    {
        printf("传入的头节点 为 NULL");
        return EOF;
    }
    listnode flag;
    listnode * list = &flag;
    if (list1->num < list2->num)
    {
        list->next = list1;
        list1 = list1->next;
    }
    else
    {
        list->next = list2;
        list2 = list2->next;
    }
    return 0;
}
int main(void)
{
    listnode * A = creatlist(A);
    listnode * B = creatlist(B);
    listnode * mergelist = mergelist(A,B);
    printlist(mergelist);
    freelist(mergelist);
    return 0;
}